from config import *
from llama_index.core.schema import NodeWithScore, MetadataMode, ImageNode,BaseNode
from llama_index.core.utils import truncate_text
from llama_index.core.schema import NodeWithScore, MetadataMode, ImageNode,TextNode
from llama_index.core.utils import truncate_text


def display_source_node(
    source_node: BaseNode,
    source_length: int = 100,
    show_source_metadata: bool = False,
    metadata_mode: MetadataMode = MetadataMode.NONE,
) -> None:
    """Display source node"""
    if isinstance(source_node,TextNode):
        text_md = (
            f"Node ID: {source_node.node_id} \n" 
            f"Text: {source_node.text} \n"
        )
        print(text_md)
        return
    if isinstance(source_node,ImageNode):
        text_md = (
            f"Node ID: {source_node.node_id} \n" 
            f"Text: {source_node.text} \n"
            f"url: {source_node.image_url} \n"
        )
        print(text_md)
        return
    elif isinstance(source_node,NodeWithScore):
        source_text_fmt = truncate_text(
            source_node.node.get_content(metadata_mode=metadata_mode).strip(), source_length
        )
        text_md = (
            f"Node ID: {source_node.node.node_id} \n"
            f"Score: {source_node.score} \n"
            f"Text: {source_text_fmt} \n"
        )
        if show_source_metadata:
            text_md += f"Metadata: {source_node.node.metadata} \n"
        if isinstance(source_node.node, ImageNode):
            text_md += "Image:"

        print(text_md)
        return
    else:
        text_md = (
            f"Text: {source_node} \n"
        )
        print(text_md)